package com.bankcomm.ui.adapter;

import java.util.List;
import com.bankcomm.R;
import com.bankcomm.bean.GroupCity;
import com.bankcomm.bean.ItemCity;
import com.bankcomm.ui.specialwidget.LPLocalCity;
import com.rytong.tools.utils.Utils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class CityNameAdapter extends BaseAdapter implements SectionIndexer {
	private List<GroupCity> list_;
	private Context context_;
	private int col_;
	private LayoutParams lp_;
	private int height ;
	private Paint paint;
	private LPLocalCity lpLocalCity;
	public CityNameAdapter(Context context,LPLocalCity lpLocalCity, List<GroupCity> groupCity, int col) {
		this.context_ = context;
		this.list_ = groupCity;
		this.col_ = col;
		
		this.lpLocalCity = lpLocalCity;
		height = Utils.getScaledValueY(30);
		this.lp_ = new LayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height));
	}

	@Override
	public int getCount() {
		return list_.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list_.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View view, ViewGroup viewGroup) {
		ViewHolder viewHolder = null;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(context_).inflate(R.layout.list_cityitem, null);
			viewHolder.tvGroupCity = (TextView) view.findViewById(R.id.city_group);
			viewHolder.llinearLayout2 = (LinearLayout) view.findViewById(R.id.ll_city2);
			viewHolder.llinearLayout2.setClickable(true);
			view.setTag(viewHolder);
		} else {
			viewHolder = (com.bankcomm.ui.adapter.CityNameAdapter.ViewHolder) view.getTag();
		}
		
		GroupCity group = list_.get(arg0);
		viewHolder.tvGroupCity.setText(group.getGroupBy());
		addCityItems(viewHolder.llinearLayout2, group.getItemCitys());
		return view;
	}
	/**
	 * 添加城市
	 * @param parent
	 * @param items
	 */
	public void addCityItems(LinearLayout parent,List<ItemCity> items){
		parent.removeAllViews();
		if(null != items && items.size() > 0){
			int size = items.size();
			int row = size/col_+(size%col_ == 0 ? 0:1);
			
			for(int i = 0;i < row;i++){
				int start = col_*i;
				int length = 0;
				if(i == (row -1)){
					length = size%col_;
					if(length == 0){
						length = col_;
					}
				}else{
					length = col_;
				}
				ItemCity[] item =  new ItemCity[length];
				for(int j = 0;j < length ;j++){
					item[j]=items.get(start+j);
				}
				CellView cell = new CellView(context_,item);					
				parent.addView(cell);
			}
		}
	}
	
	/**
	 * 自定义view
	 * @author cheng.l
	 *
	 * @date 2013-11-19 下午04:55:24
	 */
	public class CellView extends TextView{
		
		private int index = -1;
		private float w;
		private int width;
		private ItemCity[] items;
		int size = -1;
		public CellView(Context context,ItemCity[] items) {
			super(context);
			// TODO Auto-generated constructor stub
			this.setGravity(Gravity.CENTER_VERTICAL);
			this.setLayoutParams(lp_);
			this.setClickable(true);
			this.items = items;
			if(null == paint){
				paint = new Paint();
				paint.setTextSize(Utils.getFontSize(15));
				paint.setColor(Color.BLACK);
				paint.setAntiAlias(true);
			}
			
			if(null != items && items.length > 0){
				size = items.length;
			}else{
				size = -1;
			}
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			super.onDraw(canvas);
			width = getWidth();
			if(width > 0 && size > 0){
				w = width/3.0f;
				if(index >= 0 ){
					paint.setColor(0xff7896D6);
					canvas.drawRect(w*index, 0, w*(index+1), getHeight(), paint);					
				}
				paint.setColor(Color.BLACK);
				for(int i = 0;i < size;i++){
					String text = items[i].getName();
					float x = i*w+(w-paint.measureText(text))/2.0f;
					int y = ((int)(height + paint.getTextSize()))>>1;
					canvas.drawText(text, x, y, paint);
				}
			}
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			int action = event.getAction();
			int x = (int) event.getX();
			switch(action){
			case MotionEvent.ACTION_DOWN:
				if(x >= 0 && x <= w){
					index = 0;
				}else if(x > w && x <= 2*w){
					index = 1;
				}else if(x > 2*w && x <= width){
					index = 2;
				}
				//一行不足三个的情况
				if(size > 0 && index > (size-1)){
					index = -1;
				}
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				index = -1;
				
				int position = -1;
				if(x >= 0 && x <= w){
					position = 0;
				}else if(x > w && x <= 2*w){
					position = 1;
				}else if(x > 2*w && x <= width){
					position = 2;
				}
				
				if(size > 0 && position > -1 && position <= (size-1)){
					if(null != lpLocalCity){
						lpLocalCity.sendRequest(items[position]);						
					}
				}
				invalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				index = -1;
				invalidate();
				break;
			}
			return super.onTouchEvent(event);
		}
	}
	
	class ViewHolder {
		TextView tvGroupCity;
		LinearLayout llinearLayout2;
	}

	@Override
	public int getPositionForSection(int section) {
		String l = null;
		for (int i = 0; i < getCount(); i++) {
			l = list_.get(i).getGroupBy();
			char ch = l.toUpperCase().charAt(0);
			if (ch == section) {
				return i;
			}
		}
		l = null;
		return -1;
	}

	@Override
	public int getSectionForPosition(int arg0) {
		return 0;
	}

	@Override
	public Object[] getSections() {
		return null;
	}
}
